package models

import (
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

//系统字典表。对应表：t1_sys

func init() {
	//init
}

/*
故障类型模块，对应表：t1_sys
	gzlx_id为0则返回所有故障类型，或根据gzlx_id返回单个故障类型
*/
func Get_gzlx(gzlx_id int) (res []Sut_gzlx) {
	var (
		sqlstr string
		gzlx   Sut_gzlx
		stmt   *sql.Stmt
		rows   *sql.Rows
		err    error
	)
	if gzlx_id == 0 {
		//取所有故障类型
		sqlstr = `SELECT t1_sys.sys_id as id, t1_sys.sys_name as name 
					FROM t1_sys 
					WHERE t1_sys.parentId = 33 
					ORDER BY sort ASC`
		stmt, err = db.Prepare(sqlstr)
		rows, err = stmt.Query()
	} else {
		//取id号的故障类型
		sqlstr = `SELECT t1_sys.sys_id as id, t1_sys.sys_name as name 
					FROM t1_sys 
					WHERE t1_sys.sys_id = ? 
					ORDER BY sort ASC`
		stmt, err = db.Prepare(sqlstr)
		rows, err = stmt.Query(gzlx_id)
	}
	for rows.Next() {
		err = rows.Scan(&gzlx.Id, &gzlx.Name)
		checkErr(err)
		res = append(res, gzlx)
	}
	return res
}

//url:/api/v1/sblxs/all	查询所有设备的类型列表
func Get_sblxs(val int) (sblxs []Sut_sb) {
	var (
		tmp    Sut_sb
		sqlstr string
		stmt   *sql.Stmt
		rows   *sql.Rows
		err    error
	)
	//查询所有设备列表
	sqlstr = `select sb_id , name , xinghao , guige ,changjia, remark,tag, sort 
		from shebei where pid=?` //调用视图
	stmt, err = db.Prepare(sqlstr) //在数据库里PID为0的同，表示设备类型
	checkErr(err)
	rows, err = stmt.Query(val)
	checkErr(err)
	for rows.Next() {
		err = rows.Scan(&tmp.Sb_id, &tmp.Name, &tmp.Xinghao, &tmp.Guige, &tmp.Changjia, &tmp.Remark, &tmp.Tag, &tmp.Sort)
		checkErr(err)
		sblxs = append(sblxs, tmp)
	}
	return sblxs
}

//url:/api/v1/sblxs/gzlx/?id	查询工单类型（故障类型）所对应的设备列表
func Get_sblxs_gzlx(val int) (sblxs []Sut_sb) {
	var (
		tmp    Sut_sb
		sqlstr string
		stmt   *sql.Stmt
		rows   *sql.Rows
		err    error
	)
	if val == 34 || val == 60 {
		//类型为34是“使用操作”，60是“一卡通无权限”
		//返回为“无设备”的设备
		sqlstr = `SELECT
			t1_shebei.sb_id,t1_shebei.mingcheng,coalesce(t1_shebei.xinghao,""),
			coalesce(t1_shebei.guige,""),coalesce(t1_shebei.changjia,""),
			coalesce(t1_shebei.remark,""),coalesce(t1_shebei.tag,""),
			coalesce(t1_shebei.sort,0)
			FROM	t1_shebei
			WHERE		t1_shebei.sb_id = 27 AND		t1_shebei.deleted = 0
			ORDER BY		t1_shebei.sort ASC`
	}
	if val == 38 || val == 53 || val == 57 {
		//38是供电网络等故障,53是保障，57是录课，不需要对应设备
		//返回设备为“无”的设备，在数据库里ID号是27
		sqlstr = `SELECT
			t1_shebei.sb_id,t1_shebei.mingcheng,coalesce(t1_shebei.xinghao,""),
			coalesce(t1_shebei.guige,""),coalesce(t1_shebei.changjia,""),
			coalesce(t1_shebei.remark,""),coalesce(t1_shebei.tag,""),
			coalesce(t1_shebei.sort,0)
			FROM	t1_shebei
			WHERE		t1_shebei.sb_id = 27 AND		t1_shebei.deleted = 0
			ORDER BY		t1_shebei.sort ASC`
	} else if val == 35 {
		//35是软件故障，只需要列出软件
		sqlstr = `SELECT
			t1_shebei.sb_id,t1_shebei.mingcheng,coalesce(t1_shebei.xinghao,""),
			coalesce(t1_shebei.guige,""),coalesce(t1_shebei.changjia,""),
			coalesce(t1_shebei.remark,""),coalesce(t1_shebei.tag,""),
			coalesce(t1_shebei.sort,0)
			FROM	t1_shebei
			WHERE		t1_shebei.parentId = 16 AND		t1_shebei.deleted = 0
			ORDER BY		t1_shebei.sort ASC`
	} else if val == 36 || val == 56 {
		//36是硬件故障，列出所有硬件列表,56是硬件软件软故障
		sqlstr = `SELECT
			t1_shebei.sb_id,t1_shebei.mingcheng,coalesce(t1_shebei.xinghao,""),
			coalesce(t1_shebei.guige,""),coalesce(t1_shebei.changjia,""),
			coalesce(t1_shebei.remark,""),coalesce(t1_shebei.tag,""),
			coalesce(t1_shebei.sort,0)
			FROM	t1_shebei
			WHERE		t1_shebei.parentId = 24 AND		t1_shebei.deleted = 0
			ORDER BY		t1_shebei.sort ASC`
	} else if val == 37 {
		//37是巡检，列出教室类型
		sqlstr = `SELECT
			t1_shebei.sb_id,t1_shebei.mingcheng,coalesce(t1_shebei.xinghao,""),
			coalesce(t1_shebei.guige,""),coalesce(t1_shebei.changjia,""),
			coalesce(t1_shebei.remark,""),coalesce(t1_shebei.tag,""),
			coalesce(t1_shebei.sort,0)
			FROM	t1_shebei
			WHERE		t1_shebei.parentId = 29 AND		t1_shebei.deleted = 0
			ORDER BY		t1_shebei.sort ASC`
	}
	//查询所有设备列表
	stmt, err = db.Prepare(sqlstr)
	checkErr(err)
	rows, err = stmt.Query()
	checkErr(err)
	for rows.Next() {
		err = rows.Scan(&tmp.Sb_id, &tmp.Name, &tmp.Xinghao, &tmp.Guige, &tmp.Changjia, &tmp.Remark, &tmp.Tag, &tmp.Sort)
		checkErr(err)
		sblxs = append(sblxs, tmp)
	}
	return sblxs
}

/*故障处理方式，对应表t1_sys*/
func (this *Sut_sys) Get_gzclfs(type_id int) (res []Sut_sys) {
	var (
		sqlstr string
		tmp    Sut_sys
		stmt   *sql.Stmt
		rows   *sql.Rows
		err    error
	)
	if type_id == 0 {
		//取所有故障处理方式
		type_id = 41 //41是处理方式的父ID，即取所有处理方式
	} else if type_id == 57 || type_id == 38 || type_id == 37 || type_id == 56 {
		//37例行巡检，57录课，56硬件软故障，38供电\网络\线路故障,34基本处理方式
		type_id = 34
	}
	//取id号的故障类型
	sqlstr = `SELECT t1_sys.sys_id as id, t1_sys.sys_name as name 
					FROM t1_sys 
					WHERE t1_sys.type_id = ? 
					ORDER BY sort ASC`
	stmt, err = db.Prepare(sqlstr)
	rows, err = stmt.Query(type_id)
	for rows.Next() {
		err = rows.Scan(&tmp.Id, &tmp.Value)
		checkErr(err)
		res = append(res, tmp)
	}
	return res
}
